﻿using TianShu.Domain;
using TianShu.Service.Dtos;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using TianShu.Service.Contracts.Service.Crud;
using TianShu.Domain.Entities;

namespace TianShu.Service.Services.Crud
{
    public class CrudServiceImpl<TEntity, TKey, TListDto, TCreateDto, TUpdateDto, TDetailDto> :
                                    QueryServiceImpl<TEntity, TKey, TListDto, TDetailDto>,
                                    ICrudService<TKey, TListDto, TCreateDto, TUpdateDto, TDetailDto>
         where TEntity : class, IEntity<TKey>, new()
         where TListDto : class, IEntityDto
    {
        public CrudServiceImpl()
        {
        }


        public virtual async Task<PageResultDto<TDetailDto>> InsertAsync(TCreateDto dto)
        {
            var insertEntity = ObjectMapper.Map<TCreateDto, TEntity>(dto);

            insertEntity = await EntityRepository.InsertAsync(insertEntity);
            var data = ObjectMapper.Map<TEntity, TDetailDto>(insertEntity);
            return new PageResultDto<TDetailDto>(data);
        }
        public virtual async Task<PageResultDto<TDetailDto>> UpdateAsync(TKey id, TUpdateDto dto)
        {
            var dbUpdateEntity = await EntityRepository.FindAsync(id);
            if (dbUpdateEntity == null)
            {
                return new PageResultDto<TDetailDto>($"数据未找到");
            }

            var updateValidResult =await UpdateValidAsync(dbUpdateEntity, dto);
            if (updateValidResult.Success==false)
            {
                return new PageResultDto<TDetailDto>(updateValidResult.ErrorMsg);
            }

            ObjectMapper.Map(dto, dbUpdateEntity);

            dbUpdateEntity = await EntityRepository.UpdateAsync(dbUpdateEntity);
            var data = ObjectMapper.Map<TEntity, TDetailDto>(dbUpdateEntity);
            return new PageResultDto<TDetailDto>(data);
        }

        protected virtual async Task<ResultDto<bool>> UpdateValidAsync(TEntity idEntity, TUpdateDto dto)
        {
            return new ResultDto<bool>(true);
        }

        public virtual async Task<PageResultDto<bool>> DeleteAsync(IEnumerable<TKey> ids)
        {
            await EntityRepository.DeleteManyAsync(ids);
            return new PageResultDto<bool>(true);
        }
    }
    public class CrudServiceImpl<TEntity, TKey, TListDto, TCreateDto, TUpdateDto> :
                                    CrudServiceImpl<TEntity, TKey, TListDto, TCreateDto, TUpdateDto, TListDto>,
                                    ICrudService<TKey, TListDto, TCreateDto, TUpdateDto>
         where TEntity : class, IEntity<TKey>, new()
         where TListDto : class, IEntityDto
    {
        public CrudServiceImpl()
        {
        }
    }
    public class CrudServiceImpl<TEntity, TKey, TListDto, TCreateUpdateDto> :
                                    CrudServiceImpl<TEntity, TKey, TListDto, TCreateUpdateDto, TCreateUpdateDto>,
                                    ICrudService<TKey, TListDto, TCreateUpdateDto>
         where TEntity : class, IEntity<TKey>, new()
         where TListDto : class, IEntityDto
    {
        public CrudServiceImpl()
        {
        }
    }
    public class CrudServiceImpl<TEntity, TKey, TDto> :
                                    CrudServiceImpl<TEntity, TKey, TDto, TDto>,
                                    ICrudService<TKey, TDto>
         where TEntity : class, IEntity<TKey>, new()
         where TDto : class, IEntityDto
    {
        public CrudServiceImpl()
        {
        }
    }
}
